<?php
/* 用户授信表 */
namespace Home\Model;

use Common\Library\BusinessException;
use Common\Library\Constant;
use Think\Model;
use Think\Log;

class CustomerAuthModel extends Model{

    /**
     * 获取用户的认证信息
     * @param $customerId
     * @return mixed
     */
    public function getCustomerAuth($customerId)
    {
        $res = M('CustomerAuth')->where(['customer_id'=>$customerId])->find();
        
        return $res;
    }

    /**
     * 获取用户的芝麻分信息
     * @param $customerId
     * @return mixed
     */
    public function getCustomerzhima($customerId)
    {
        $res = M('Customer')->field("sesame_score")->where(['id'=>$customerId])->select();
        
        return $res;
    }

    /**
     * [isNeedContactsVerify 判断是否需要个人基础数据信息重填]
     * @return boolean [description]
     */
    public function isNeedContactsVerify($customer_id)
    {
        //拒贷用户流程
        $ConsumeHistoryRefuseInfo = M('ConsumeHistory')->field('customer_id, type, status, add_time')->where("customer_id = '$customer_id' and type = 4 and status = 6")->order('add_time desc')->find();
            //无拒贷记录 续贷用户
        if (empty($ConsumeHistoryRefuseInfo) ) {//当没有拒贷信息时，调用基础信息是否需要重填方法
            $returnStatus = $this->checkExtraInfo($customer_id);
        }
            //有拒贷记录
        $refuseGapTime = time() - $ConsumeHistoryRefuseInfo['add_time'];
        if ($refuseGapTime > 30*86400) {
            $returnStatus = $this->checkExtraInfo($customer_id);
        } else {//拒贷没超过30天的进入拒贷页面
            $returnStatus = 0;
        }
        //续贷用户流程(目前没有严重逾期或者不良标签的判断)
        // $ConsumeHistoryInfo = M('ConsumeHistory')->field('id, customer_id, type, status, repayment_time, add_time')->where("customer_id = '$customer_id' and type = 5 and status = 3")->order('add_time desc')->find();
        // $consume_id = $ConsumeHistoryInfo['id'];
        // $ConsumeInstallmentHistoryInfo = M('ConsumeInstallmentHistory')->field('repayment_time, update_time')->where("parent_id = '$consume_id' and status = 1")->order('add_time desc')->find();
        // $repayment_time = $ConsumeInstallmentHistoryInfo['repayment_time'];
        // $update_time = $ConsumeInstallmentHistoryInfo['update_time'];
        // if ($update_time < $repayment_time) {//最近一条完成的借款没逾期，调用基础信息是否需要重填方法
        //     $returnStatus = $this->checkExtraInfo($customer_id);
        // } else {
        //     //逾期了，判断是否严重逾期，调用基础信息是否需要重填方法
        //     $returnStatus = 0;
        // }
        

        if ($returnStatus) {
            return 0;//需要认证个人基础信息资料
        } else {
            return 1;//不需要
        }
    }

    /**
     * [checkExtraInfo 校验是否基础数据完整]
     * @return [type] [description]
     */
    public function checkExtraInfo($customer_id){
        // try{
            // $$customer_id = I('post.customer_id');
            if(!$customer_id || $customer_id<=0){
                BusinessException::throwException(Constant::INVALID_PARAMETER);
            }
            /**查询新版本是否需要进行附加信息重新认证**/
            $result = M('CustomerExtraInfo')->field('extra_info_json')->where(['customer_id'=>$customer_id])->find();
            /**用户没有进行重新认证,需要重新添加附加信息**/
            if( empty($result['extra_info_json']) ){
                $ret = 1;//返回1，需要补全  基础信息
                // $ret = Constant::getReturn(Constant::SUCCESS);
            } else {
                $ret = 0;//返回0，基础信息完整  不需要补全
            }
        // }catch (BusinessException $e){
        //     $ret = ['code'=>$e->getCode(), 'msg'=>$e->getMessage()];
        // }
        
        return $ret;
        // $ret = Response::format($ret);
        // $this->ajaxReturn($ret);
    }

    /**
     * 解析用户当前的认证信息
     */
    public function getCustomerAuthInfo($customer)
    {
        $auth = $this->getCustomerAuth($customer['id']);
        Log::write(json_encode($auth), "INFO");
       /************|旧版认证步骤|* //认证步骤：0-未认证  1-联系人信息已经上传 2-已身份认证 3-已活体检测认证***********/
        //认证步骤：0-新用户   1-身份证认证   2-活体认证   3-绑定银行卡   4-填写个人信息
        $customer['credit_step'] = 0;
        // $extra = M('CustomerExtraInfo')->where(['customer_id'=>$customer['id']])->find();
        $bank = M('CustomerBankInfo')->where(['customer_id'=>$customer['id']])->select();
        $extra = $this->isNeedContactsVerify($customer['id']);
        if ($auth['id_num'] == 1){
            $customer['credit_step'] = 1;
            if ($auth['face_pass'] == 1){
                $customer['credit_step'] = 2;
                if ($bank){
                    $customer['credit_step'] = 3;
                    if ($extra){
                        $customer['credit_step'] = 4;
                    }
                }
            }
        }
        $customer['isset_pay_pass'] = $customer['pay_pass'] ? 1 : 0; //是否设置了密码：0-未设置 1-已设置
        $customer['create_time'] = $customer['add_time'];
        $customer['max_amount'] = Constant::MAX_CREDIT_AMOUNT;
        $customer['china_mobile'] = isset($auth['china_mobile']) ? $auth['china_mobile'] : 0;
        $customer['ali_pay'] = isset($auth['ali_pay']) ? $auth['ali_pay'] : 0;
        $customer['jd_pass'] = isset($auth['jd_pass']) ? $auth['jd_pass'] : 0;

        //剔除不需要的出参信息
        unset($customer['tongdun_device_id']);
        unset($customer['tongdun_application_id']);
        unset($customer['office_tel']);
        unset($customer['password']);
        unset($customer['pay_pass']);
        unset($customer['latest_location']);
        unset($customer['status']);
        unset($customer['add_time']);
        unset($customer['gender']);
        unset($customer['nation']);
        unset($customer['is_local']);
        unset($customer['in_white_list']);
        unset($customer['birthday']);
        unset($customer['birthplace']);
        unset($customer['sign_organ']);
        unset($customer['valid_period']);
        unset($customer['qq']);
        unset($customer['company']);
        unset($customer['order_count']);
        unset($customer['type']);
        unset($customer['overdue_count']);

        return $customer;
    }


    /**getCustomerAuthInfo
     * 记录用户的认证结果信息
     * @param $data
     * @return mixed
     */
    public function insCustomerAuth($data)
    {
        if(!($ins = M('CustomerAuth')->add($data))){
            BusinessException::throwException(Constant::INTERNAL_ERROR_MYSQL);
        }

        return $ins;
    }
    /**
     * 修改用户的运营商认证
     * @param $custimerId
     * @return bool
     */
    public function saveCustomerAuth($custimerId)
    {
        $data['china_mobile'] = 0;
        $res = M('CustomerAuth')->where(['customer_id'=>$custimerId])->save($data);

        return $res;
    }
}